<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Service Check Parallelization</title>

<STYLE type="text/css">
<!--
        .Default { font-family: verdana,arial,serif; font-size: 8pt; }
        .PageTitle { font-family: verdana,arial,serif; font-size: 12pt; font-weight: bold; }
-->      
</STYLE>

</head>

<body bgcolor="#FFFFFF" text="black" class="Default">

<p>
<div align="center">
<h2 class="PageTitle">Service Check Parallelization</h2>
</div>
</p>

<hr>

<p>
<strong><u>Introduction</u></strong>
</p>
<p>
One of the features of Nagios is its ability to execute service checks in parallel.  This documentation will attempt to explain in detail what that means and how it affects services that you have defined.  
</p>

<p>
<strong><u>How The Parallelization Works</u></strong>
</p>

<p>
Before I can explain how the service check parallelization works, you first have to understand a bit about how Nagios schedules events.  All internal events in Nagios (i.e. log file rotations, external command checks, service checks, etc.) are placed in an event queue.  Each item in the event queue has a time at which it is scheduled to be executed.  Nagios does its best to ensure that all events get executed when they should, although events may fall behind schedule if Nagios is busy doing other things.
</p>

<p>
Service checks are one type of event that get scheduled in Nagios' event queue.  When it comes time for a service check to be executed, Nagios will kick off another process (using a call to fork()) to go out and run the service check (i.e. a plugin of some sort).  Nagios does <i>not</i>, however, wait for the service check to finish!  Instead, Nagios will immediately go back to servicing other events that reside in the event queue...
</p>

<p>
So what happens when the service check finishes executing?  Well, the process that was started by Nagios to run the service check sends a message back to Nagios containing the results of the service check.  It is then up to Nagios to check for and process the results of that service check when it gets a chance.
</p>

<p>
In order for Nagios to actually do any monitoring, it must process the results of service checks that have finished executing.  This is done via a service check "reaper" process.  Service "reapers" are another type of event that get scheduled in Nagios' event queue.  The frequency of these "reaper" events is determined by the <a href="configmain.html#service_reaper_frequency">service_reaper_frequency</a> option in the main configuration file.  When a "reaper" event is executed, it will check for any messages that contain the result of service checks that have finished executing.  These service check results are then handled by the core service monitoring logic.  From there Nagios determines whether or not hosts should be checked, notifications should be sent out, etc.  When the service check results have been processed, Nagios will reschedule the next check of the service and place it in the event queue for later execution.  That completes the service check/monitoring cycle!
</p>

<p>
For those of you who really want to know, but haven't looked at the code, Nagios uses message queues to handle communication between Nagios and the process that actually runs the service check...
</p>

<p>
<strong><u>Potential Gotchas...</u></strong>
</p>
<p>
You should realize that there are potential drawbacks to having service checks parallelized.  Since more than one service check may be running at the same time, they have may interfere with one another.  You'll have to evaluate what types of service checks you're running and take appropriate steps to guard against any unfriendly outcomes.  This is particularly important if you have more than one service check that accesses any hardware (like a modem).  Also, if two or more service checks connect to daemon on a remote host to check some information, make sure that daemon can handle multiple simultaneous connections.
</p>

<p>
Fortunately, there are some things you can do to protect against problems with having some types of service checks "collide"...
</p>
<p>
<ol>
<li>The easiest thing you can do to prevent service check collisions to to use the <a href="configmain.html#service_interleave_factor">service_interleave_factor</a> variable.  Interleaving services will help to reduce the load imposed upon remote hosts by service checks.  Set the variable to use "smart" interleave factor calculation and then adjust it manually if you find it necessary to do so.
<li>The second thing you can do is to set the <i>max_check_attempts</i> argument in each service definition to something greater than one.  If the service check does happen to collide with another running check, Nagios will retry the service check <i>max_check_attempts-1</i> times before notifying anyone of a problem.  
<li>You could try is to implement some kind of "back-off and retry" logic in the actual service check code, although you may find it difficult or too time-consuming
<li>If all else fails you can effectively prevent service checks from being parallelized by setting the <a href="configmain.html#max_concurrent_checks">max_concurrent_checks</a> option to 1.  This will allow only one service to be checked at a time, so it isn't a spectacular solution.  If there is enough demand, I will add an option to the service definitions which will allow you to specify on a per-service basis whether or not a service check can be parallelized.  If there isn't enough demand, I won't...
</ol>
</p>

<p>
One other thing to note is the effect that parallelization of service checks can have on system resources on the machine that runs Nagios.  Running a lot of service checks in parallel can be taxing on the CPU and memory.  The <a href="configmain.html#inter_check_delay_method">inter_check_delay_method</a> will attempt to minimize the load imposed on your machine by spreading the checks out evenly over time (if you use the "smart" method), but it isn't a surefire solution.  In order to have some control over how many service checks can be run at any given time, use the <a href="configmain.html#max_concurrent_checks">max_concurrent_checks</a> variable.  You'll have to tweak this value based on the total number of services you check, the system resources you have available (CPU speed, memory, etc.), and other processes which are running on your machine.  For more information on how to tweak the <i>max_concurrent_checks</i> variable for your setup, read the documentation on <a href="checkscheduling.html#max_concurrent_checks">check scheduling</a>.
</p>


<p>
<strong><u>What Isn't Parallelized</u></strong>
</p>
<p>
It is important to remember that only the <i>execution</i> of service checks has been parallelized.  There is good reason for this - other things cannot be parallelized in a very safe or sane manner.  In particular, event handlers, contact notifications, processing of service checks, and host checks are <i>not</i> parallelized.  Here's why...
</p>

<p>
<i>Event handlers</i> are not parallelized because of what they are designed to do.  Much of the power of event handlers comes from the ability to do proactive problem resultion.  An example of this is restarting the web server when the HTTP service on the local machine is detected as being down.  In order to prevent more than one event handler from trying to "fix" problems in parallel (without any knowledge of what each other is doing), I have decided to not parallelize them.
</p>

<p>
<i>Contact notifications</i> are not parallelized because of potential notification methods you may be using.  If, for example, a contact notification uses a modem to dial out and send a message to your pager, it requires exclusive access to the modem while the notification is in progress.  If two or more such notifications were being executed in parallel, all but one would fail because the others could not get access to the modem.  There are ways to get around this, like providing some kind of "back-off and retry" method in the notification script, but I've decided not to rely on users having implemented this type of feature in their scripts.  One quick note - if you have service checks which use a modem, make sure that any notification scripts that dial out have some method of retrying access to the modem.  This is necessary because a service check may be running at the same time a notification is!
</p>

<p>
<i>Processing of service check results</i> has not been parallelized.  This has been done to prevent situations where multiple notifications about host problems or recoveries may be sent out if a host goes down, becomes unreachable, or recovers.
</p>

<hr>

</body>
</html>